﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ELab.Serialization.Data;
using ELab.ImageProcess;

namespace ELab.Controls
{
    public partial class InforControl : UserControl
    {
        private bool isViewed = false, isLocked = false;
        private string controlMeActive = "Ct-me", controlMeUnactive = "End";
        private Color onlineColor = System.Drawing.Color.FromArgb(239, 101, 0), offlineColor = System.Drawing.Color.FromArgb(111, 111, 111);
        public InforControl()
        {
            InitializeComponent();
            this.IsShowLogout = false;
            btnStop.Visible = false;
            this.Cursor = Cursors.Hand;
            btnControlMe.Text = controlMeActive;
        }

        public event EventHandler ViewOnlyRequested;
        public event EventHandler RemoteRequested;
        public event EventHandler StopViewOrRemoteRequested;
        public event EventHandler LockRequested;
        public event EventHandler UnlockRequested;
        public event EventHandler ControlMeRequested;
        public event EventHandler Actived;


        private User _CurrentUser = null;
        public User CurrentUser
        {
            get { return _CurrentUser; }
            set
            {
                _CurrentUser = value;
                if (_CurrentUser == null) return;
                this.Avatar = ImageExtensions.FromByte(_CurrentUser.Avatar);
                this.TeacherName = _CurrentUser.Fullname;
                this.IPAddress = _CurrentUser.IP;
            }
        }

        public event EventHandler OnUserLogout;

        public Image Avatar
        {
            get
            {
                return this.picAvatar.Image;
            }
            set
            {
                this.picAvatar.Image = value;
            }
        }

        public string TeacherName
        {
            get
            {
                return this.lblTeacherName.Text;
            }
            set
            {
                this.lblTeacherName.Text = value;
            }
        }

        public string StudentsCount
        {
            get
            {
                return this.lblStudents.Text;
            }
            set
            {
                this.lblStudents.Text = value;
                UpdateButtonStatus();
            }
        }

        public string IPAddress
        {
            get
            {
                return this.lblIPAddress.Text;
            }
            set
            {
                this.lblIPAddress.Text = value;
            }
        }


        public bool IsShowLogout
        {
            get
            {
                return this.btnLogout.Visible;
            }
            set
            {
                this.btnLogout.Visible = value;
                UpdateButtonStatus();
            }
        }

        private void UpdateButtonStatus()
        {
            this.btnStop.Visible = false;
            this.btnRemote.Visible = false;
            this.btnView.Visible = false;
            this.btnLock.Visible = false;
            this.btnControlMe.Visible = false;
            if (this.lblStudents.Text == "Offline")
            {
                lblTeacherName.ForeColor = offlineColor;
                this.Cursor = Cursors.Default;
            }
            else
            {
                this.Cursor = Cursors.Hand;
                lblTeacherName.ForeColor = onlineColor;
                if (!this.IsShowLogout && isActive)
                {
                    if (this.CurrentUser != null && CurrentUser.Type == UserType.Student)
                    {
                        this.btnLock.Visible = true;
                        this.btnControlMe.Visible = true;
                    }
                    if (isViewed)
                    {
                        this.btnRemote.Visible = false;
                        this.btnView.Visible = false;
                        this.btnStop.Visible = true;
                    }
                    else
                    {
                        if (this.CurrentUser != null && CurrentUser.Type == UserType.Student)
                        {
                            this.btnRemote.Visible = true;
                            this.btnView.Visible = true;
                        }
                        this.btnStop.Visible = false;
                    }
                }
            }
        }

        private bool isActive = false;
        public bool IsActive
        {
            get
            {
                return isActive;
            }
            set
            {
                isActive = value;
                UpdateButtonStatus();
                if (isActive & CurrentUser != null && CurrentUser.Type != UserType.Teacher)
                {
                    this.BackColor = SystemColors.ActiveCaption;
                }
                else
                {
                    this.BackColor = Color.Transparent;
                }
            }
        }

        private void btnLogOut_Click(object sender, EventArgs e)
        {
            if (OnUserLogout != null) OnUserLogout(sender, e);
            if (Actived != null) Actived(this, e);
        }

        private void btnView_Click(object sender, EventArgs e)
        {
            isViewed = true;
            if (ViewOnlyRequested != null) ViewOnlyRequested(this, e);
            if (Actived != null) Actived(this, e);
        }

        private void btnRemote_Click(object sender, EventArgs e)
        {
            isViewed = true;
            if (RemoteRequested != null) RemoteRequested(this, e);
            if (Actived != null) Actived(this, e);
        }

        private void btnLock_Click(object sender, EventArgs e)
        {
            if (!isLocked)
            {
                isLocked = true;
                btnLock.Text = "Unlock";
                btnLock.BackColor = Color.FromArgb(0, 169, 224);
                if (LockRequested != null) LockRequested(this, e);
            }
            else
            {
                isLocked = false;
                btnLock.Text = "Lock";
                btnLock.BackColor = Color.FromArgb(246, 99, 73);
                if (UnlockRequested != null) UnlockRequested(this, e);
            }

            if (Actived != null) Actived(this, e);
        }

        private void InforControl_Click(object sender, EventArgs e)
        {
            if (Actived != null) Actived(this, e);
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            isViewed = false;
            if (StopViewOrRemoteRequested != null) StopViewOrRemoteRequested(this, e);
            if (Actived != null) Actived(this, e);
        }

        private void btnControlMe_Click(object sender, EventArgs e)
        {
            if (btnControlMe.Text == controlMeActive)
            {
                btnControlMe.Text = controlMeUnactive;
            }
            else
            {
                btnControlMe.Text = controlMeActive;
            }

            //todo
            if (ControlMeRequested != null) ControlMeRequested(this, e);

            //end
            if (Actived != null) Actived(this, e);
        }
    }
}
